namespace Core
{
    using System;
    using Base;
    using Contracts.Interfaces;
using System.ComponentModel;
    using System.Diagnostics;

    
    public sealed class Fitting : PropertyChangedBase, IFitting, IEquatable<IFitting>
    {
        private string _name;
        private IRobot _robot;
        [field:NonSerialized]
        private bool _isDirty;

        private Fitting(){ }
        /// <summary>
        /// Creates a new instance
        /// </summary>
        /// <param name="name">The fitting name</param>
        /// <param name="robot">The fitting IRobot instance or null</param>
        public Fitting(string name, IRobot robot =null)
        {
            if(string.IsNullOrEmpty(name))throw new ArgumentNullException("name");
            _name = name;
            _robot = robot;

            if(_robot!=null)
                _robot.PropertyChanged += OnRobotPropertyChanged;
        }

        
        /// <summary>
        /// Obtains or defines the fitting name
        /// </summary>
        public string Name
        {
            get { return _name; }
            set
            {
                _name = value;
                IsDirty = true;
                NotifyPropertyChange(()=>Name);
            }
        }

        /// <summary>
        /// Obtains or defines the fitting robot
        /// </summary>
        public IRobot Robot
        {
            get { return _robot; }
            set
            {
                if (_robot != null)
                    _robot.PropertyChanged -= OnRobotPropertyChanged;

                _robot = value;

                if(_robot!=null)
                    _robot.PropertyChanged += OnRobotPropertyChanged;

                IsDirty = true;
                NotifyPropertyChange(()=>Robot);
            }
        }

        /// <summary>
        /// Obtains or defines if the instance has pending changes
        /// </summary>
        public bool IsDirty
        {
            get { return _isDirty; }
            set { _isDirty = value; NotifyPropertyChange(()=>IsDirty); }
        }
        /// <summary>
        /// IRobot instance PropertyChanged event handler
        /// </summary>
        private void OnRobotPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if(string.IsNullOrEmpty(e.PropertyName)) return;
            IsDirty = true;
        }
        /// <summary>
        /// Indicates whether the current object is equal to another object of the same type.
        /// </summary>
        /// <returns>
        /// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.
        /// </returns>
        /// <param name="other">An object to compare with this object.</param>
        public bool Equals(IFitting other)
        {
            return other != null && String.Equals(other.Name, _name, StringComparison.InvariantCultureIgnoreCase);
        }

        /// <summary>
        /// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// true if the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>; otherwise, false.
        /// </returns>
        /// <param name="obj">The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>. </param><filterpriority>2</filterpriority>
        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            return Equals(obj as IFitting);
        }

        /// <summary>
        /// Serves as a hash function for a particular type. 
        /// </summary>
        /// <returns>
        /// A hash code for the current <see cref="T:System.Object"/>.
        /// </returns>
        /// <filterpriority>2</filterpriority>
        public override int GetHashCode()
        {
            return _name.GetHashCode();
        }

        /// <summary>
        /// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </returns>
        /// <filterpriority>2</filterpriority>
        public override string ToString()
        {
            return String.Format("{0} ({1})", _name, _robot );
        }
    }
}